package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.service.IStraregyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.common.query.TravelQueryObject;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResult;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class IndexController {

    @Reference
    private ITravelCommendService travelCommendService;
    @Reference
    private IStraregyCommendService straregyCommendService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IStrategySearchService strategySearchService;
    @Reference
    private ITravelSearchService travelSearchService;
    @Reference
    private IUserInfoSearchService userInfoSearchService;
    @Reference
    private IDestinationSearchService destinationSearchService;
    @Reference
    private ISearchService searchService;

    @RequestMapping("")
    public String index(Model model){
        //tcs 游记推荐 5篇
        model.addAttribute("tcs", travelCommendService.listTop5());
        //scs 攻略推荐 5篇
        model.addAttribute("scs",straregyCommendService.listTop5());
        return "index/index";
    }

    @RequestMapping("/index/travelPage")
    public String travelPage(@ModelAttribute("qo") TravelQueryObject qo , Model model){
        model.addAttribute("pageInfo",travelService.query(qo));
        return "index/travelPageTpl";
    }

    @RequestMapping("q")
    public String query(@ModelAttribute("qo") SearchQueryObject qo, Model model){
        //如果客户没有输入任何数据就按查询
        if (qo.getKeyword() == null) {
           qo.setKeyword(" ");
        }
        String viewName = null;
        switch (qo.getType()){
            //查询目的地
            case SearchQueryObject.SEARCH_DEST:
                searchDest(qo.getKeyword(),model);
                viewName = "searchDest";
                break;
            case SearchQueryObject.SEARCH_STRATEGY:
                searchStrategy(qo,model);
                viewName = "searchStrategy";
                break;
            case SearchQueryObject.SEARCH_TRAVEL:
                searchTravel(qo,model);
                viewName = "searchTravel";
                break;
            case SearchQueryObject.SEARCH_USER:
                searchUser(qo,model);
                viewName = "searchUser";
                break;
            default:
                searchAll(qo,model);
                viewName = "searchAll";
                break;
        }
        return "index/"+viewName;
    }

    private void searchAll(SearchQueryObject qo, Model model) {
        SearchResult data = new SearchResult();
        int length = 0;

        Page<UserInfoTemplate> users = searchService.hightLightSearch(UserInfoTemplate.INDEX_TYPE_NAME, UserInfoTemplate.class,
                qo, "nickname", "destName");
        length += users.getTotalElements();

        Page<TravelTemplate> travels = searchService.hightLightSearch(TravelTemplate.INDEX_TYPE_NAME, TravelTemplate.class,
                qo, "title", "subTitle");
        length += travels.getTotalElements();

        Page<StrategyTemplate> strategys = searchService.hightLightSearch(StrategyTemplate.INDEX_TYPE_NAME, StrategyTemplate.class,
                qo, "title", "subTitle", "summary");
        length += strategys.getTotalElements();

        data.setTotal(length);
        data.setUsers(users.getContent());
        data.setTravels(travels.getContent());
        data.setStrategys(strategys.getContent());

        model.addAttribute("data",data);
    }

    private void searchUser(SearchQueryObject qo, Model model) {
        Page<UserInfoTemplate> page = searchService.hightLightSearch(UserInfoTemplate.INDEX_TYPE_NAME, UserInfoTemplate.class,
                qo, "nickname", "destName");
        model.addAttribute("page",page);
    }

    private void searchTravel(SearchQueryObject qo, Model model) {
        Page<TravelTemplate> page = searchService.hightLightSearch(TravelTemplate.INDEX_TYPE_NAME, TravelTemplate.class,
                qo, "title", "subTitle");
        model.addAttribute("page",page);
    }

    private void searchStrategy(SearchQueryObject qo, Model model) {
        Page<StrategyTemplate> page = searchService.hightLightSearch(StrategyTemplate.INDEX_TYPE_NAME, StrategyTemplate.class,
                qo, "title", "subTitle", "summary");
        model.addAttribute("page",page);
    }

    private void searchDest(String keyword, Model model) {
        if(keyword == null || "".equals(keyword.trim())){
            model.addAttribute("dest",null);
            model.addAttribute("data",null);
        }
        //dest
        DestinationTemplate dest = destinationSearchService.getByName(keyword);

        //data[strategys,travels,users, total]
        SearchResult data = new SearchResult();

        List<StrategyTemplate> strategys = strategySearchService.listByDestName(keyword);
        List<TravelTemplate> travels = travelSearchService.listByDestName(keyword);
        List<UserInfoTemplate> users = userInfoSearchService.listByDestName(keyword);

        // 拓展: 当数据量大的时候, 要适当限制
        data.setStrategys(strategys);
        data.setTravels(travels);
        data.setUsers(users);

        int length = strategys == null ? 0 : strategys.size() +
                (travels == null ? 0 : travels.size()) +
                (users == null ? 0 : users.size());
        data.setTotal(length + (dest == null ? 0 : 1));

        model.addAttribute("dest",dest);
        model.addAttribute("data",data);
    }

}
